home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / contrib / ezview / ezview.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-06  |  5.0 KB  |  181 lines

  1. /*
  2. Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
  3.  
  4. Permission to use, copy, modify, and distribute this material 
  5. for any purpose and without fee is hereby granted, provided 
  6. that the above copyright notice and this permission notice 
  7. appear in all copies, and that the name of Bellcore not be 
  8. used in advertising or publicity pertaining to this 
  9. material without the specific, prior written permission 
  10. of an authorized representative of Bellcore.  BELLCORE 
  11. MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
  12. OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
  13. WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
  14. */
  15. #include <stdio.h>
  16. #include <mail.h> /* from andrew distribution */
  17. #ifdef    SYSV
  18. #include <sys/types.h>
  19. /* Different people say different things about whether unistd.h lives in sys/ */
  20. /* #include <sys/unistd.h> */
  21. #include <unistd.h>
  22. #include <sys/fcntl.h>
  23. #include <sys/termio.h>
  24. #endif
  25. #include <sys/file.h>
  26. #include <config.h>
  27.  
  28. #ifdef AMIGA
  29. #define BUFFER_SIZE (BUFSIZ+1)
  30. #else
  31. #define BUFFER_SIZE 5000
  32. extern char **environ;
  33. #endif
  34.  
  35. main(argc, argv)
  36. int argc;
  37. char **argv;
  38. {
  39.     struct ScribeState ScribeState;
  40.     FILE *infp;
  41.     int bytes, code;
  42.     char *prog = "ez", *fname;
  43.     char TmpName[100];
  44.     char BigBuf[BUFFER_SIZE];
  45. #ifndef AMIGA
  46.     char adir[1200], *s;
  47. #endif
  48.     
  49.     if (argc < 2) exit(-1);
  50.     if (argc > 2) {
  51.     prog = argv[1];
  52.     fname = argv[2];
  53.     } else {
  54.     fname = argv[1];
  55.     }
  56.     TmpName[0] = 0;
  57.     if (!strcmp(fname, "-")) {
  58.         FILE *fp;
  59.         int c;
  60.  
  61. #ifdef AMIGA
  62.         strcpy(TmpName, "T:mmXXXXXX");
  63.         mktemp(TmpName);
  64. #else
  65.         sprintf(TmpName, "/tmp/ezview.%d", getpid());
  66. #endif
  67.         fp = fopen(TmpName, "w");
  68.         if (!fp) exit(-1);
  69.         while ((c = getc(stdin)) != EOF) putc(c, fp);
  70.         fclose(fp);
  71.         fname = TmpName;
  72.     }
  73. #ifndef AMIGA
  74.     sprintf(BigBuf, "%s -geometry +0+0 -d %s", prog, fname);
  75.     if (!getenv("ANDREWDIR")) {
  76.         /* Add *some* ANDREWDIR variable, to help with the fonts */
  77.         if (argc >2) {
  78.             sprintf(adir, "ANDREWDIR=%s", argv[1]);
  79.             s = (char *) rindex(adir, '/');
  80.             if (s) {
  81.                 *s = NULL;
  82.                 s = (char *) rindex(adir, '/');
  83.                 if (s) *s = NULL;
  84.             }
  85.         } else {
  86.             if (!access("/usr/andrew", R_OK)) {
  87.                 sprintf(adir, "ANDREWDIR=%s", "/usr/andrew");
  88.             } else if (!access("/usr/local/pkg/X11/andrew", R_OK)) {
  89.                 sprintf(adir, "ANDREWDIR=%s", "/usr/local/pkg/X11/andrew");
  90.             } else {
  91.                 adir[0] = NULL; /* Hope for compiled-in default! */
  92.             }
  93.         }
  94.         if (adir[0]) {
  95.             /* Add to the environment here */
  96.             char **newenviron;
  97.             int environsize;
  98.             for (environsize=0; environ[environsize]; ++environsize) {;}
  99.             newenviron = (char **) malloc(sizeof(char *) * (2+environsize));
  100.             newenviron[environsize+1] = NULL;
  101.             newenviron[environsize] = adir;
  102.             while (environsize--) {
  103.                 newenviron[environsize] = environ[environsize];
  104.             }
  105.             environ = newenviron;
  106.         }
  107.     }
  108.     if (!getenv("DISPLAY") || RunCmd(BigBuf))
  109. #endif
  110.     {
  111.         infp = fopen(fname, "r");
  112.         if (infp == NULL) {
  113.             fprintf(stderr, "%s: Unable to open \"%s\" for input.\n", argv[0],
  114.                     fname);
  115.             if (TmpName[0]) unlink(TmpName);
  116.             exit(10);
  117.         }
  118.     code = UnScribeInit("12", &ScribeState);
  119.         while ((bytes = fread(BigBuf, 1, sizeof(BigBuf) - 1, infp)) > 0) {
  120.         UnScribe(code, &ScribeState, BigBuf, bytes, stdout);
  121.     }
  122.     UnScribeFlush(code, &ScribeState, stdout);
  123.         fclose(infp);
  124.     }
  125.     if (TmpName[0]) unlink(TmpName);
  126.     exit(0);
  127. }
  128.  
  129. /* The following returns static data which is overwritten with each call; it must therefore be used only once in a sprintf, for example. */
  130. static char *RepresentChar(c)
  131. char c;
  132. {
  133.     static char ans[20];
  134.  
  135.     if (c == '\015') {
  136.     strcpy(ans, "RETURN");
  137.     } else if (c <= '\032') {
  138.     sprintf(ans, "CTL-%c", c+'A'-1);
  139.     } else if (c == '\033') {
  140.     strcpy(ans, "ESC");
  141.     } else if (c == '\177') {
  142.     strcpy(ans, "DELETE/RUBOUT");
  143.     } else {
  144.     ans[0] = c;
  145.     ans[1] = '\0';
  146.     }
  147.     return(ans);
  148. }
  149.  
  150. #ifndef AMIGA
  151. #ifdef __hpux
  152. #include <termio.h>
  153. #else
  154. #include <sys/ioctl.h>
  155. #endif
  156.  
  157. RunCmd(BigBuf)
  158. char *BigBuf;
  159. {
  160.     char abort = '\177';
  161. #ifdef __hpux
  162. #define SYSV    /* Is this necessary? */
  163. #endif
  164. #ifdef SYSV
  165.     struct termio blob;
  166.  
  167.     if (!ioctl(0, TCGETA, &blob)) {
  168.         abort = blob.c_cc[VINTR];
  169.     }
  170. #else
  171.     struct tchars blob;
  172.  
  173.     if (!ioctl(0, TIOCGETC, &blob)) {
  174.         abort = blob.t_intrc;
  175.     }
  176. #endif
  177.     fprintf(stderr, "Now executing the EZ program -- if an EZ window appears, you can quit it by\n    holding down the middle mouse button and selecting the 'Quit' menu.\n\nYou can prevent the window from appearing by INTERRUPTING (%s) now,\n    in which case you should see a text-only version of the data.\n", RepresentChar(abort));
  178.     return(system(BigBuf));
  179. }
  180. #endif
  181.